package com.todo.event.activity.application.handler.activityrule;

import com.todo.event.activity.application.entity.context.ActivityRuleContext;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

/**
 * @author azhebuxing
 * @date 2025/1/13 15:37
 * @description
 */
@Slf4j
@Component
public class ActivityRuleHandleChain {

    /**
     * 定义chain的头和尾
     */
    private AbstractActivityRuleHandle head = null;
    private AbstractActivityRuleHandle tail = null;

    /**
     * 增加处理结点
     *
     * @param handler
     */
    public void addHandler(AbstractActivityRuleHandle handler) {
        handler.setSuccessor(null);
        if (head == null) {
            head = handler;
            tail = handler;
            return;
        }
        tail.setSuccessor(handler);
        tail = handler;
    }

    public void execute(ActivityRuleContext context) {
        head.ruleHandle(context);
    }
}
